home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / AbstractListModel.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  148 lines

  1. /*
  2.  * @(#)AbstractListModel.java    1.11 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import com.sun.java.swing.event.*;
  24. import java.io.Serializable;
  25.  
  26. /**
  27.  * The Abstract definition for the data model the provides
  28.  * a List with its contents.
  29.  * <p>
  30.  * Warning: serialized objects of this class will not be compatible with
  31.  * future swing releases.  The current serialization support is appropriate 
  32.  * for short term storage or RMI between Swing1.0 applications.  It will
  33.  * not be possible to load serialized Swing1.0 objects with future releases
  34.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  35.  * baseline for the serialized form of Swing objects.
  36.  */
  37. public abstract class AbstractListModel implements ListModel, Serializable
  38. {
  39.     protected EventListenerList listenerList = new EventListenerList();
  40.  
  41.  
  42.     /**
  43.      * Add a listener to the list that's notified each time a change
  44.      * to the data model occurs.
  45.      * @param l the ListDataListener
  46.      */  
  47.     public void addListDataListener(ListDataListener l) {
  48.     listenerList.add(ListDataListener.class, l);
  49.     }
  50.  
  51.  
  52.     /**
  53.      * Remove a listener from the list that's notified each time a 
  54.      * change to the data model occurs.
  55.      * @param l the ListDataListener
  56.      */  
  57.     public void removeListDataListener(ListDataListener l) {
  58.     listenerList.remove(ListDataListener.class, l);
  59.     }
  60.  
  61.  
  62.     /**
  63.      * AbstractListModel subclasses must call this method <b>after</b>
  64.      * one or more elements of the list change.  The changed elements
  65.      * are specified by a closed interval index0, index1, i.e. the
  66.      * range that includes both index0 and index1.  Note that
  67.      * index0 need not be less than or equal to index1.
  68.      * 
  69.      * @param source The ListModel that changed, typically "this".
  70.      * @param index0 One end of the new interval.
  71.      * @param index1 The other end of the new interval.
  72.      * @see EventListenerList
  73.      * @see DefaultListModel
  74.      */
  75.     protected void fireContentsChanged(Object source, int index0, int index1)
  76.     {
  77.     Object[] listeners = listenerList.getListenerList();
  78.     ListDataEvent e = null;
  79.  
  80.     for (int i = listeners.length - 2; i >= 0; i -= 2) {
  81.         if (listeners[i] == ListDataListener.class) {
  82.         if (e == null) {
  83.             e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, index0, index1);
  84.         }
  85.         ((ListDataListener)listeners[i+1]).contentsChanged(e);
  86.         }           
  87.     }
  88.     }
  89.  
  90.  
  91.     /*
  92.      * AbstractListModel subclasses must call this method <b>after</b>
  93.      * one or more elements are added to the model.  The new elements
  94.      * are specified by a closed interval index0, index1, i.e. the
  95.      * range that includes both index0 and index1.  Note that
  96.      * index0 need not be less than or equal to index1.
  97.      * 
  98.      * @param source The ListModel that changed, typically "this".
  99.      * @param index0 One end of the new interval.
  100.      * @param index1 The other end of the new interval.
  101.      * @see EventListenerList
  102.      * @see DefaultListModel
  103.      */
  104.     protected void fireIntervalAdded(Object source, int index0, int index1)
  105.     {
  106.     Object[] listeners = listenerList.getListenerList();
  107.     ListDataEvent e = null;
  108.  
  109.     for (int i = listeners.length - 2; i >= 0; i -= 2) {
  110.         if (listeners[i] == ListDataListener.class) {
  111.         if (e == null) {
  112.             e = new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, index0, index1);
  113.         }
  114.         ((ListDataListener)listeners[i+1]).intervalAdded(e);
  115.         }           
  116.     }
  117.     }
  118.  
  119.  
  120.     /*
  121.      * AbstractListModel subclasses must call this method <b>after</b>
  122.      * one or more elements are removed from the model.  The new elements
  123.      * are specified by a closed interval index0, index1, i.e. the
  124.      * range that includes both index0 and index1.  Note that
  125.      * index0 need not be less than or equal to index1.
  126.      * 
  127.      * @param source The ListModel that changed, typically "this".
  128.      * @param index0 One end of the new interval.
  129.      * @param index1 The other end of the new interval.
  130.      * @see EventListenerList
  131.      * @see DefaultListModel
  132.      */
  133.     protected void fireIntervalRemoved(Object source, int index0, int index1)
  134.     {
  135.     Object[] listeners = listenerList.getListenerList();
  136.     ListDataEvent e = null;
  137.  
  138.     for (int i = listeners.length - 2; i >= 0; i -= 2) {
  139.         if (listeners[i] == ListDataListener.class) {
  140.         if (e == null) {
  141.             e = new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, index0, index1);
  142.         }
  143.         ((ListDataListener)listeners[i+1]).intervalRemoved(e);
  144.         }           
  145.     }
  146.     }
  147. }
  148.